<?php

class Gallery{
	
	/**
	 * Instanse
	 *
	 * @var Gallery
	 */
	private static $instanse;
	private static $curId;
	private $data = array();
	private $photos = array();
	
	private function __construct($id){
		self::$curId = $id;
		$q = "SELECT * FROM tb_gallery_photo WHERE parent_id = {$id} ORDER BY weight desc";
		$r = mysql_query($q);
		
		while ($data = mysql_fetch_assoc($r)) {
			$this->photos[$data['id']] = $data;
		}
		
		$q = "SELECT * FROM tb_gallery WHERE id = '{$id}'";
		$this->data = mysql_fetch_assoc(mysql_query($q));	
		$this->data['count'] = sizeof($this->photos);	
	}
	/**
	 * Enter description here...
	 *
	 * @param int $id
	 * @return Gallery
	 */
	public static function getInstanse($id){
		
		if(!FormValidator::IsInt($id)){
			throw new Exception("Id musq by int value");
		}
		
		if(is_null(self::$instanse) || $id != $this->curId){
			self::$instanse = new Gallery($id);
		}
		
		return self::$instanse;
	}
	
	public function getPhotos(){
		return $this->photos;
	}
	
	public function getData(){
		return $this->data;
	}
}